import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:jump_scape/js_ui/js_activity/js_about.dart';
import 'package:jump_scape/js_ui/js_activity/js_coin_shop.dart';
import 'package:jump_scape/js_ui/js_activity/js_home.dart';
import 'package:jump_scape/js_ui/js_activity/js_launch.dart';
import 'package:jump_scape/js_ui/js_activity/js_login.dart';
import 'package:jump_scape/js_ui/js_activity/js_game.dart';
import 'package:jump_scape/js_ui/js_activity/js_personal.dart';
import 'package:jump_scape/js_ui/js_activity/js_records.dart';
import 'package:jump_scape/js_ui/js_activity/js_setting.dart';
import 'package:jump_scape/js_ui/js_activity/js_topic.dart';
import 'package:jump_scape/js_ui/js_widget/js_blur_group.dart';
import 'package:jump_scape/js_ui/js_widget/js_image_view.dart';
import 'package:jump_scape/js_ui/js_widget/js_text_ghosting_view.dart';
import 'package:jump_scape/js_ui/js_widget/js_title_view.dart';

import 'js_config.dart';
import 'js_ui/js_activity/js_personal_edit.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  PaintingBinding.instance.imageCache.maximumSizeBytes = 1024 * 1024 * 300;

  runApp(const MainApp(true));
}

class MainApp extends StatelessWidget {
  final bool isFirstLaunchApp;
  const MainApp(this.isFirstLaunchApp, {super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 375),
      minTextAdapt: false,
      splitScreenMode: false,
      builder: ((context, child) {
        return GetMaterialApp(
          debugShowCheckedModeBanner: false,
          title: JsApp.appName,
          darkTheme: ThemeData.dark().copyWith(
            highlightColor: Colors.transparent,
            hoverColor: Colors.transparent,
            splashColor: Colors.transparent,
          ),
          theme: ThemeData.light().copyWith(
            highlightColor: Colors.transparent,
            hoverColor: Colors.transparent,
            splashColor: Colors.transparent,
          ),
          themeMode: ThemeMode.light,
          onGenerateRoute: _onGenerateRoute,
          onUnknownRoute: _onUnknownRoute,
          builder: ((context, child) {
            return MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: child!,
            );
          }),
          home: child,
        );
      }),
      child: JsLaunchActivity(),
    );
  }

  Route<dynamic>? _onGenerateRoute(RouteSettings settings) {
    final name = settings.name ?? '';

    Widget? widget;
    switch (name) {
      case JsLaunchActivity.url:
        widget = const JsLaunchActivity();
        break;
      case JsLoginActivity.url:
        widget = const JsLoginActivity();
        break;
      case JsHomeActivity.url:
        widget = const JsHomeActivity();
        break;
      case JsTopicActivity.url:
        widget = const JsTopicActivity();
        break;
      case JsGameActivity.url:
        final index = settings.arguments as int;
        widget = JsGameActivity(index: index);
        break;
      case JsRecordsActivity.url:
        widget = const JsRecordsActivity();
        break;
      case JsPersonalEditActivity.url:
        widget = const JsPersonalEditActivity();
        break;
      case JsPersonalActivity.url:
        widget = const JsPersonalActivity();
        break;
      case JsCoinShopActivity.url:
        widget = const JsCoinShopActivity();
        break;
      case JsAboutActivity.url:
        widget = const JsAboutActivity();
        break;
      case JsSettingActivity.url:
        widget = const JsSettingActivity();
        break;
    }

    if (widget == null) return null;
    return MaterialPageRoute(builder: (context) => widget!);
  }

  Route<dynamic> _onUnknownRoute(RouteSettings settings) {
    final name = settings.name ?? '';
    return MaterialPageRoute(
      builder: (context) => Scaffold(
        body: WillPopScope(
          onWillPop: () => Future.value(false),
          child: Scaffold(
            body: Stack(
              children: [
                jsImage('assets/image/ic_login_bg.png', width: double.infinity, height: double.infinity, fit: BoxFit.cover),
                JsTitleView(title: '404'),
                Center(
                  child: JsBlurGroup(
                    width: double.infinity,
                    height: 259.r,
                    radius: 30.r,
                    margin: EdgeInsets.symmetric(horizontal: 34.r),
                    padding: EdgeInsets.symmetric(horizontal: 24.r, vertical: 12.r),
                    child: JsTextGhostingView('Not found {$name}', fontSize: 24.r),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
